package exercise1;

import javax.swing.JOptionPane;

public class EasyThread implements Runnable {
	public static int j = 0;
	private Object monitor;

	public EasyThread(Object monitor) {
		this.monitor = monitor;
	}

	@Override
	public void run() {
		while (true) {
			criticalSelection();
		}
	}

	private void criticalSelection() {

		synchronized (monitor) {
			System.out.println(Thread.currentThread().getName());

			try {
				Thread.currentThread().sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	public static void main(String args[]) {
		j = Integer
				.valueOf(JOptionPane.showInputDialog("Wie viele Threads???"));

		Object monitor = new Object();

		for (int i = 0; i <= j; i++) {
			EasyThread et = new EasyThread(monitor);
			Thread t = new Thread(et);
			t.start();
		}

	}
}
